package com.wiyun.engine.skeleton;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.res.AssetManager;
import android.view.MotionEvent;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.Animation.IAnimationCallback;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.INodeVirtualMethods;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;

import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.tmx.TMXTileMap;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.types.WYSize;
import com.wiyun.engine.utils.ResolutionIndependent;

public class TMXMapLayer extends Layer implements IAnimationCallback, INodeVirtualMethods {
	public enum AnimationDir
	{
		left,
		right,
		up,
		down,
		lface,
		rface,
		uface,
		dface;
	}
	AssetManager m_asset;
	public TMXMapLayer(AssetManager am) throws IOException
	{
		m_asset=am;
		CreateMap();
		try {
			CreatePlayer();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	TMXTileMap mMap;
	
	private float mLastX;
	private float mLastY;
	private float moveCache=3;
	private float dir_left=0,dir_leftup=1,dir_up=2,dir_upright=3,
			dir_right=4,dir_rightdown=5,dir_down=6,dir_downleft=7;
	private Animation player_left_anim, player_right_anim, player_up_anim, player_down_anim, 
	player_upright_anim, player_rightdown_anim, player_leftup_anim, player_downleft_anim, 
	player_leftface_anim, player_rightface_anim, player_upface_anim, player_downface_anim, 
	player_uprightface_anim, player_rightdownface_anim, player_leftupface_anim, player_downleftface_anim;
	String role="player_nanzhanshi";
	Sprite mPlayer;
	float animDurp=0.15f;
	
	public void CreateMap()
	{
		/*Texture2D tex11 = Texture2D.makeJPG(R.drawable.bg_00x00);
		Texture2D tex12 = Texture2D.makeJPG(R.drawable.bg_00x01);
		Texture2D tex13 = Texture2D.makeJPG(R.drawable.bg_00x02);
		Texture2D tex14 = Texture2D.makeJPG(R.drawable.bg_00x03);
		Texture2D tex15 = Texture2D.makeJPG(R.drawable.bg_00x04);
		Texture2D tex16 = Texture2D.makeJPG(R.drawable.bg_00x05);
		
		Texture2D tex21 = Texture2D.makeJPG(R.drawable.bg_01x00);
		Texture2D tex22 = Texture2D.makeJPG(R.drawable.bg_01x01);
		Texture2D tex23 = Texture2D.makeJPG(R.drawable.bg_01x02);
		Texture2D tex24 = Texture2D.makeJPG(R.drawable.bg_01x03);
		Texture2D tex25 = Texture2D.makeJPG(R.drawable.bg_01x04);
		Texture2D tex26 = Texture2D.makeJPG(R.drawable.bg_01x05);
		
		Texture2D tex31 = Texture2D.makeJPG(R.drawable.bg_02x00);
		Texture2D tex32 = Texture2D.makeJPG(R.drawable.bg_02x01);
		Texture2D tex33 = Texture2D.makeJPG(R.drawable.bg_02x02);
		Texture2D tex34 = Texture2D.makeJPG(R.drawable.bg_02x03);
		Texture2D tex35 = Texture2D.makeJPG(R.drawable.bg_02x04);
		Texture2D tex36 = Texture2D.makeJPG(R.drawable.bg_02x05);
		
		Texture2D tex41 = Texture2D.makeJPG(R.drawable.bg_03x00);
		Texture2D tex42 = Texture2D.makeJPG(R.drawable.bg_03x01);
		Texture2D tex43 = Texture2D.makeJPG(R.drawable.bg_03x02);
		Texture2D tex44 = Texture2D.makeJPG(R.drawable.bg_03x03);
		Texture2D tex45 = Texture2D.makeJPG(R.drawable.bg_03x04);
		Texture2D tex46 = Texture2D.makeJPG(R.drawable.bg_03x05);
		tex11.autoRelease();
		tex12.autoRelease();
		tex13.autoRelease();
		tex14.autoRelease();
		tex15.autoRelease();
		tex16.autoRelease();

		tex21.autoRelease();
		tex22.autoRelease();
		tex23.autoRelease();
		tex24.autoRelease();
		tex25.autoRelease();
		tex26.autoRelease();

		tex31.autoRelease();
		tex32.autoRelease();
		tex33.autoRelease();
		tex34.autoRelease();
		tex35.autoRelease();
		tex36.autoRelease();

		tex41.autoRelease();
		tex42.autoRelease();
		tex43.autoRelease();
		tex44.autoRelease();
		tex45.autoRelease();
		tex46.autoRelease();
		mMap = TMXTileMap.make(R.raw.a3,tex11, tex12,tex13,tex14,tex15,tex16,
		tex21, tex22,tex23,tex24,tex25,tex26,
		tex31, tex32,tex33,tex34,tex35,tex36,
		tex41, tex42,tex43,tex44,tex45,tex46);
		mMap.setDebugDrawObjects(true);
        addChild(mMap);
        
        setTouchEnabled(true);*/
	}

	Animation CreateAnimationFromTexList(List<Texture2D> list, int id, float durp)
	{
		Animation anim;
		if(list.size()==2)
			anim = new Animation(id, durp, list.get(0), list.get(1));
		else if(list.size()==3)
			anim = new Animation(id, durp, list.get(0), list.get(1), list.get(2));
		else if(list.size()==4)
			anim = new Animation(id, durp, list.get(0), list.get(1), list.get(2), list.get(3));
		else if(list.size()==5)
			anim = new Animation(id, durp, list.get(0), list.get(1), list.get(2), list.get(3), list.get(4));
		else if(list.size()==6)
			anim = new Animation(id, durp, list.get(0), list.get(1), list.get(2), list.get(3), list.get(4), list.get(5));
		else if(list.size()==7)
			anim = new Animation(id, durp, list.get(0), list.get(1), list.get(2), list.get(3), list.get(4), list.get(5), list.get(6));
		else if(list.size()==8)
			anim = new Animation(id, durp, list.get(0), list.get(1), list.get(2), list.get(3), list.get(4), list.get(5), list.get(6), list.get(7));
		else if(list.size()==9)
			anim = new Animation(id, durp, list.get(0), list.get(1), list.get(2), list.get(3), list.get(4), list.get(5), list.get(6), list.get(7), list.get(8));
		else// if(list.size()==10)
			anim = new Animation(id, durp, list.get(0), list.get(1), list.get(2), list.get(3), list.get(4), list.get(5), list.get(6), list.get(7), list.get(8), list.get(9));
		
		/*anim = (Animation)new Animation(0, 0.3f, 
            	R.drawable.a_up1,
            	R.drawable.a_up2,
            	R.drawable.a_up3,
            	R.drawable.a_up4,
            	R.drawable.a_up5,
            	R.drawable.a_up6,
            	R.drawable.a_up7,
            	R.drawable.a_up8);*/
		anim.setCallback(this);
		return anim;
	}
	Animation LoadAnimation(String folderName,int id, float durp) throws IOException
	{
		String[] files = m_asset.list(folderName);
		List<Texture2D> texList = new ArrayList<Texture2D>();
		for(int i=0;i<files.length;i++)
		{
			Texture2D tex = Texture2D.makePNG(folderName + File.separator + files[i]);
			texList.add(tex);
		}
		return CreateAnimationFromTexList(texList, id, durp);
	}
	Label mLabel;
	AnimationRole _playerAnimation;
	float _speed=0;
	public void CreatePlayer() throws IOException
	{
		WYSize s = Director.getInstance().getWindowSize();
		mPlayer = Sprite.make(Texture2D.makePNG(R.drawable.a_up1));
     	mPlayer. setAnchorY(0);
		mPlayer.setPosition(100, 100);
		addChild(mPlayer);
     	mPlayer.scale(0.75f);
		//player_left_anim = LoadAnimation(role+File.separator+"run_left", 0, animDurp);
		_playerAnimation = new AnimationRole(role, 0.15f, _speed, this, m_asset); 
		mLabel = Label.make("callback display label", 20);
        mLabel.setPosition(100, 150);
        addChild(mLabel);
		//IntervalAction anim = (IntervalAction)Animate.make(player_left_anim, true).autoRelease();
	}
	 @Override
     public boolean wyTouchesBegan(MotionEvent event) {
		
	     	mLastX = event.getX();
	     	mLastY = event.getY();
	     	WYPoint loc = Director.getInstance().convertToGL(event.getX(), event.getY());
	     	_playerAnimation.RunAnimation(mPlayer, loc.x, loc.y);
	     	return true;
     }
     
     @Override
     public boolean wyTouchesMoved(MotionEvent event) {
     	// remember android event origin is not same as OpenGL
     	// you can use Director.getInstance().convertToGL(event.getX(), event.getY()) to convert it to GL coordinate
     	float x = event.getX();
     	float y = event.getY();
     	float deltaX = x - mLastX;
     	float deltaY = y - mLastY; 
     	if(Math.abs(mLastX-x)>moveCache || Math.abs(mLastY-y)>moveCache)
		{
	     	mLastX = x;
	     	mLastY = y;
	     	mMap.translate(deltaX, -deltaY);
		}
     	return true;
     }
     
     public void onAnimationEnded(int animPointer) {
			//mLabel.setText("animation ended");
    	 
		}
     	float _playerLastX=100, _playerLastY=100;
		public void onAnimationFrameChanged(int animPointer, int index) {
			//mLabel.setText("frame changed: " + index);
			float x = mPlayer.getPositionX(), y = mPlayer.getPositionY();
			if(x!=_playerLastX || y!=_playerLastY)
			{
				mMap.translate(_playerLastX-x, _playerLastY-y);
				mMap.translateChildren(_playerLastX-x,  _playerLastY-y);
			}
		}

		@Override
		public void jDraw() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void jOnEnter() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void jOnEnterTransitionDidFinish() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void jOnExit() {
			// TODO Auto-generated method stub
			_playerAnimation.Dispose();
		}
}
